[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-panel-devel
Subject:    battery applet - charge time instead of charge percentage.
From:       Marcos Dione <mdione () grulic ! org ! ar>
Date:       2009-01-06 0:14:03
Message-ID: 20090106001402.GG12989 () mustang ! grulicueva ! net
[Download RAW message or body]

as I mentioned in IRC, I have a patch for this. as sebas and aseigo
suggested, I'm sending it here.

    now, right now there are 3 bugs: the remaining time, either for
total discharge when the AC is plugged off and the complete recharge
time when the AC is plugged back in.

    the first one is that the time jumps a lot depending on the laptop's
laod. just IRC'ing and lkistening to music makes it change from 3h to
2:50. not a big gap, but misleading anyways. I haven't tested yet what
happens when the charge is really low. the second one is that the time
'till the battery is fully recharged starts very big, like 6h, then
settles to more real time, like 10m. both problems lay deeper than this
applet, they come from powerdevil or even deeper, I just didn't check.

    the third one is avoidable, really. I coded it so when the battery
is fully charged it shows 'Charged' instead of 0:00. the problem is that
right now it doesn't set its width (not again!) so the tect fits. this
can be 'solved' just removing an simple if until I hack the final code.

    so, whithout further addo, here it is. as always, comments are more
than welcome.

-- 
(Not so) Random fortune:
Maebe: Do you guys know where I could get one of those gold T-shaped
    pendants?
Michael: That's a cross.
Maebe: Across from where?
	    -- Arrested development

["battery_applet-show_remaining_time.diff" (text/x-diff)]

Index: kdebase/workspace/plasma/applets/battery/battery.h
===================================================================
--- kdebase/workspace/plasma/applets/battery/battery.h	(revision 904784)
+++ kdebase/workspace/plasma/applets/battery/battery.h	(working copy)
@@ -120,6 +120,8 @@
         bool m_showMultipleBatteries;
         /* Should the battery charge information be shown on top? */
         bool m_showBatteryString;
+        /* Should that info be percentage (false) or time (true)? */
+        bool m_showRemainingTime;
         QSizeF m_size;
         int m_pixelSize;
         Plasma::Svg* m_theme;
Index: kdebase/workspace/plasma/applets/battery/battery.cpp
===================================================================
--- kdebase/workspace/plasma/applets/battery/battery.cpp	(revision 904784)
+++ kdebase/workspace/plasma/applets/battery/battery.cpp	(working copy)
@@ -87,6 +87,7 @@
       m_firstRun(true),
       m_numOfBattery(0),
       m_acadapter_plugged(false),
+      m_showRemainingTime(false),
       m_remainingMSecs(0)
 {
     kDebug() << "Loading applet battery";
@@ -217,6 +218,11 @@
     connect(parent, SIGNAL(applyClicked()), this, SLOT(configAccepted()));
     connect(parent, SIGNAL(okClicked()), this, SLOT(configAccepted()));
     ui.showBatteryStringCheckBox->setChecked(m_showBatteryString ? Qt::Checked : \
Qt::Unchecked); +    if (m_showRemainingTime) {
+        ui.showTimeRadioButton->setChecked(Qt::Checked);
+    } else {
+        ui.showPercentageRadioButton->setChecked(Qt::Checked);
+    }
     ui.showMultipleBatteriesCheckBox->setChecked(m_showMultipleBatteries ? \
Qt::Checked : Qt::Unchecked);  }
 
@@ -224,6 +230,16 @@
 {
     KConfigGroup cg = config();
 
+    if (m_showRemainingTime != ui.showTimeRadioButton->isChecked()) {
+        kDebug() << "config changed";
+        m_showRemainingTime = !m_showRemainingTime;
+        cg.writeEntry("showRemainingTime", m_showRemainingTime);
+        kDebug() << m_showRemainingTime;
+        if (m_showBatteryString && m_showBatteryString == \
ui.showBatteryStringCheckBox->isChecked()) { +            \
showLabel(m_showBatteryString); +        }
+    }
+
     if (m_showBatteryString != ui.showBatteryStringCheckBox->isChecked()) {
         m_showBatteryString = !m_showBatteryString;
         cg.writeEntry("showBatteryString", m_showBatteryString);
@@ -491,13 +507,15 @@
     if (m_numOfBattery && m_batteryLabel) {
         QHashIterator<QString, QHash<QString, QVariant > > \
battery_data(m_batteries_data);  int bnum = 0;
-        int hours = m_remainingMSecs/1000/3600;
-        int minutes = qRound(m_remainingMSecs/60000) % 60;
+        // int hours = m_remainingMSecs/1000/3600;
+        // int minutes = qRound(m_remainingMSecs/60000) % 60;
 
         while (battery_data.hasNext()) {
             bnum++;
             battery_data.next();
             QString state = battery_data.value()["State"].toString();
+            m_remainingMSecs = battery_data.value()["Remaining msec"].toInt();
+            // kDebug() << "time left:" << m_remainingMSecs;
             if (state == "Discharging" && m_remainingMSecs > 0) {
 
                 // FIXME: Somehow, m_extenderApplet is null here, so the label never \
becomes visible @@ -506,9 +524,9 @@
                 }
 
                 // we don't have too much accuracy so only give hours and minutes
-                int msecs = hours * 1000 * 3600 + minutes * 60000;
-                batteryLabelText.append(i18n("Time remaining: <b>%1</b><br />", \
                KGlobal::locale()->prettyFormatDuration(msecs)));
-                kDebug() << "hours:" << hours << "minutes:" << minutes;
+                // int msecs = hours * 1000 * 3600 + minutes * 60000;
+                batteryLabelText.append(i18n("Time remaining: <b>%1</b><br />", \
KGlobal::locale()->prettyFormatDuration(m_remainingMSecs))); +                // \
kDebug() << "hours:" << hours << "minutes:" << minutes;  /* might be useful for the \
tooltip  kDebug() << "hours:" << hours << "minutes:" << minutes;
                 QTime t = QTime(hours, minutes);
@@ -865,8 +883,32 @@
                 // Show the charge percentage with a box on top of the battery
                 QString batteryLabel;
                 if (battery_data.value()["Plugged in"].toBool()) {
-                    batteryLabel = battery_data.value()["Percent"].toString();
-                    batteryLabel.append("%");
+                    kDebug() << m_showRemainingTime;
+                    if (!m_showRemainingTime) {
+                        batteryLabel = battery_data.value()["Percent"].toString();
+                        batteryLabel.append("%");
+                    } else {
+                        // TODO: make it wider so it can show the time properly
+                        // int msecs= battery_data.value()["Remaining \
msec"].toInt(); +                        m_remainingMSecs = \
battery_data.value()["Remaining msec"].toInt(); +                        if \
(m_remainingMSecs>0) { +                            int hours = \
m_remainingMSecs/1000/3600; +                            int minutes = \
qRound(m_remainingMSecs/60000) % 60; +                            QTime t = \
QTime(hours, minutes); +                            // kDebug() << t;
+                            KLocale tmpLocale(*KGlobal::locale());
+                            // nicer alternative, but have to implement the above \
TODO +                            // tmpLocale.setTimeFormat("%kh %Mm");
+                            tmpLocale.setTimeFormat("%k:%M");
+                            // too wide!
+                            // batteryLabel = \
KGlobal::locale()->prettyFormatDuration(msecs); +                            \
batteryLabel = tmpLocale.formatTime(t, false, true); // minutes, hours as duration +  \
} else { +                            // too wide, implement above TODO
+                            batteryLabel = i18n("Charged");
+                        }
+                    }
+                    // kDebug() << batteryLabel;
                     paintLabel(p, corect, batteryLabel);
                 }
             }
Index: kdebase/workspace/plasma/applets/battery/batteryConfig.ui
===================================================================
--- kdebase/workspace/plasma/applets/battery/batteryConfig.ui	(revision 904784)
+++ kdebase/workspace/plasma/applets/battery/batteryConfig.ui	(working copy)
@@ -6,7 +6,7 @@
     <x>0</x>
     <y>0</y>
     <width>363</width>
-    <height>80</height>
+    <height>270</height>
    </rect>
   </property>
   <property name="windowTitle" >
@@ -22,11 +22,70 @@
       <string/>
      </property>
      <property name="text" >
-      <string>Show the percentage of &amp;charge on the battery</string>
+      <string>Show charge &amp;information</string>
      </property>
     </widget>
    </item>
    <item>
+    <layout class="QGridLayout" name="gridLayout" >
+     <item row="0" column="1" >
+      <widget class="QRadioButton" name="showPercentageRadioButton" >
+       <property name="enabled" >
+        <bool>false</bool>
+       </property>
+       <property name="text" >
+        <string>Show &amp;percentage</string>
+       </property>
+       <property name="checked" >
+        <bool>true</bool>
+       </property>
+      </widget>
+     </item>
+     <item row="0" column="0" >
+      <spacer name="horizontalSpacer" >
+       <property name="orientation" >
+        <enum>Qt::Horizontal</enum>
+       </property>
+       <property name="sizeType" >
+        <enum>QSizePolicy::Fixed</enum>
+       </property>
+       <property name="sizeHint" stdset="0" >
+        <size>
+         <width>20</width>
+         <height>20</height>
+        </size>
+       </property>
+      </spacer>
+     </item>
+     <item row="1" column="0" >
+      <spacer name="horizontalSpacer_2" >
+       <property name="orientation" >
+        <enum>Qt::Horizontal</enum>
+       </property>
+       <property name="sizeType" >
+        <enum>QSizePolicy::Fixed</enum>
+       </property>
+       <property name="sizeHint" stdset="0" >
+        <size>
+         <width>20</width>
+         <height>20</height>
+        </size>
+       </property>
+      </spacer>
+     </item>
+     <item row="1" column="1" >
+      <widget class="QRadioButton" name="showTimeRadioButton" >
+       <property name="enabled" >
+        <bool>false</bool>
+       </property>
+       <property name="text" >
+        <string>Show remaining &amp;time</string>
+       </property>
+      </widget>
+     </item>
+    </layout>
+   </item>
+   <item>
     <widget class="QCheckBox" name="showMultipleBatteriesCheckBox" >
      <property name="toolTip" >
       <string/>
@@ -52,5 +111,38 @@
   </layout>
  </widget>
  <resources/>
- <connections/>
+ <connections>
+  <connection>
+   <sender>showBatteryStringCheckBox</sender>
+   <signal>toggled(bool)</signal>
+   <receiver>showPercentageRadioButton</receiver>
+   <slot>setEnabled(bool)</slot>
+   <hints>
+    <hint type="sourcelabel" >
+     <x>181</x>
+     <y>20</y>
+    </hint>
+    <hint type="destinationlabel" >
+     <x>195</x>
+     <y>52</y>
+    </hint>
+   </hints>
+  </connection>
+  <connection>
+   <sender>showBatteryStringCheckBox</sender>
+   <signal>toggled(bool)</signal>
+   <receiver>showTimeRadioButton</receiver>
+   <slot>setEnabled(bool)</slot>
+   <hints>
+    <hint type="sourcelabel" >
+     <x>181</x>
+     <y>20</y>
+    </hint>
+    <hint type="destinationlabel" >
+     <x>195</x>
+     <y>83</y>
+    </hint>
+   </hints>
+  </connection>
+ </connections>
 </ui>



_______________________________________________
Plasma-devel mailing list
Plasma-devel@kde.org
https://mail.kde.org/mailman/listinfo/plasma-devel


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic