From kde-panel-devel Tue Jan 06 00:14:03 2009 From: Marcos Dione Date: Tue, 06 Jan 2009 00:14:03 +0000 To: kde-panel-devel Subject: battery applet - charge time instead of charge percentage. Message-Id: <20090106001402.GG12989 () mustang ! grulicueva ! net> X-MARC-Message: https://marc.info/?l=kde-panel-devel&m=123120104330884 MIME-Version: 1 Content-Type: multipart/mixed; boundary="--HcAYCG3uE/tztfnV" --HcAYCG3uE/tztfnV Content-Type: text/plain; charset=us-ascii Content-Disposition: inline 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 --HcAYCG3uE/tztfnV Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="battery_applet-show_remaining_time.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 > 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: %1
", KGlobal::locale()->prettyFormatDuration(msecs))); - kDebug() << "hours:" << hours << "minutes:" << minutes; + // int msecs = hours * 1000 * 3600 + minutes * 60000; + batteryLabelText.append(i18n("Time remaining: %1
", 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 @@ 0 0 363 - 80 + 270 @@ -22,11 +22,70 @@ - Show the percentage of &charge on the battery + Show charge &information + + + + + false + + + Show &percentage + + + true + + + + + + + Qt::Horizontal + + + QSizePolicy::Fixed + + + + 20 + 20 + + + + + + + + Qt::Horizontal + + + QSizePolicy::Fixed + + + + 20 + 20 + + + + + + + + false + + + Show remaining &time + + + + + + @@ -52,5 +111,38 @@ - + + + showBatteryStringCheckBox + toggled(bool) + showPercentageRadioButton + setEnabled(bool) + + + 181 + 20 + + + 195 + 52 + + + + + showBatteryStringCheckBox + toggled(bool) + showTimeRadioButton + setEnabled(bool) + + + 181 + 20 + + + 195 + 83 + + + + --HcAYCG3uE/tztfnV Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Plasma-devel mailing list Plasma-devel@kde.org https://mail.kde.org/mailman/listinfo/plasma-devel --HcAYCG3uE/tztfnV--