source: lumina/lumina-desktop/panel-plugins/battery/LBattery.cpp @ 0e77750

releng/10.0.3releng/10.1
Last change on this file since 0e77750 was 0e77750, checked in by Ken Moore <ken@…>, 4 months ago

Large update to the Lumina Panel Plugins:
1) Now all the buttons/plugins should resize to fit the panel appropriately
2) All buttons/plugins operate when the panel is either horizontal *or* vertical3) Task Manager buttons will now only display the icon for a window if the application supplies one (similar to windows/mac, application icons are assumed to be unique). If an icon could *not* be found, it will provide a generic one as well as list the application on the button (to distinguish between multiple apps that show the same icon). The application name is also set as the tooltip for the button, so users can easily check what app an icon refers to if necessary.

  • Property mode set to 100644
File size: 3.6 KB
Line 
1//===========================================
2//  Lumina-DE source code
3//  Copyright (c) 2014, Susanne Jaeckel
4//  Available under the 3-clause BSD license
5//  See the LICENSE file for full details
6//===========================================
7#include "LBattery.h"
8
9LBattery::LBattery(QWidget *parent, QString id, bool horizontal) : LPPlugin(parent, id, horizontal){
10  iconOld = -1;
11  //Setup the widget
12  label = new QLabel(this);
13    label->setScaledContents(true);
14    //label->setAlignment(Qt::AlignCenter);
15  this->layout()->addWidget(label);
16  //Setup the timer
17  timer = new QTimer();
18  timer->setInterval(5000); //update every 5 seconds
19  connect(timer,SIGNAL(timeout()), this, SLOT(updateBattery()) );
20  timer->start();
21  QTimer::singleShot(0,this,SLOT(OrientationChange()) ); //update the sizing/icon
22}
23
24LBattery::~LBattery(){
25  timer->stop();
26  delete timer;
27}
28
29void LBattery::updateBattery(bool force){
30  // Get current state of charge
31  //QStringList result = LUtils::getCmdOutput("/usr/sbin/apm", QStringList() << "-al");
32  int charge = SYSTEM::batteryCharge(); //result.at(1).toInt();
33//qDebug() << "1: " << result.at(0).toInt() << " 2: " << result.at(1).toInt();
34  int icon = -1;
35  if (charge > 90) { icon = 4; }
36  else if (charge > 70) { icon = 3; }
37  else if (charge > 50) { icon = 2; }
38  else if (charge > 30) { icon = 1; }
39  else if (charge > 0 ) { icon = 0; }
40  if(SYSTEM::batteryIsCharging()){ icon = icon+10; }
41  //icon = icon + result.at(0).toInt() * 10;
42  if (icon != iconOld || force) {
43    switch (icon) {
44      case 0:
45        label->setPixmap( LXDG::findIcon("battery-caution", "").pixmap(label->size()) );
46        break;
47      case 1:
48        label->setPixmap( LXDG::findIcon("battery-040", "").pixmap(label->size()) );
49        break;
50      case 2:
51        label->setPixmap( LXDG::findIcon("battery-060", "").pixmap(label->size()) );
52        break;
53      case 3:
54        label->setPixmap( LXDG::findIcon("battery-080", "").pixmap(label->size()) );
55        break;
56      case 4:
57        label->setPixmap( LXDG::findIcon("battery-100", "").pixmap(label->size()) );
58        break;
59      case 10:
60        label->setPixmap( LXDG::findIcon("battery-charging-caution", "").pixmap(label->size()) );
61        break;
62      case 11:
63        label->setPixmap( LXDG::findIcon("battery-charging-040", "").pixmap(label->size()) );
64        break;
65      case 12:
66        label->setPixmap( LXDG::findIcon("battery-charging-060", "").pixmap(label->size()) );
67        break;
68      case 13:
69        label->setPixmap( LXDG::findIcon("battery-charging-080", "").pixmap(label->size()) );
70        break;
71      case 14:
72        label->setPixmap( LXDG::findIcon("battery-charging", "").pixmap(label->size()) );
73        break;
74      default:
75        label->setPixmap( LXDG::findIcon("battery-missing", "").pixmap(label->size()) );
76        break;
77    }
78    iconOld = icon;
79  }
80  //Now update the display
81  QString tt;
82  //Make sure the tooltip can be properly translated as necessary (Ken Moore 5/9/14)
83  if(icon > 9 && icon < 15){ tt = QString(tr("%1 % (Charging)")).arg(QString::number(charge)); }
84  else{ tt = QString( tr("%1 % (%2 Remaining)") ).arg(QString::number(charge), getRemainingTime() ); }
85  label->setToolTip(tt);
86}
87
88QString LBattery::getRemainingTime(){
89  int secs = SYSTEM::batterySecondsLeft();
90  if(secs < 0){ return "??"; }
91  QString rem; //remaining
92  if(secs > 3600){
93    int hours = secs/3600;
94    rem.append( QString::number(hours)+"h ");
95    secs = secs - (hours*3600);
96  }
97  if(secs > 60){
98    int min = secs/60;
99    rem.append( QString::number(min)+"m ");
100    secs = secs - (min*60);
101  }
102  if(secs > 0){
103    rem.append(QString::number(secs)+"s");
104  }
105  return rem;
106}
Note: See TracBrowser for help on using the repository browser.