1
/***************************************************************************
2
* Copyright (C) 2005,2006,2007 by Siraj Razick <siraj@kdemail.net> *
3
* Copyright (C) 2007 by Riccardo Iaconelli <ruphy@fsfe.org> *
4
* Copyright (C) 2007 by Sebastian Kuegler <sebas@kde.org> *
6
* This program is free software; you can redistribute it and/or modify *
7
* it under the terms of the GNU General Public License as published by *
8
* the Free Software Foundation; either version 2 of the License, or *
9
* (at your option) any later version. *
11
* This program is distributed in the hope that it will be useful, *
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14
* GNU General Public License for more details. *
16
* You should have received a copy of the GNU General Public License *
17
* along with this program; if not, write to the *
18
* Free Software Foundation, Inc., *
19
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
20
***************************************************************************/
24
#include <QApplication>
26
#include <QStyleOptionGraphicsItem>
28
#include <QGraphicsSceneHoverEvent>
32
#include <KLocalizedString>
33
#include <KSharedConfig>
35
#include <KColorScheme>
36
#include <KGlobalSettings>
38
#include <plasma/svg.h>
39
#include <plasma/theme.h>
41
Battery::Battery(QObject *parent, const QVariantList &args)
42
: Plasma::Applet(parent, args),
43
m_battery_percent_label(0),
49
kDebug() << "Loading applet battery";
50
setAcceptsHoverEvents(true);
51
setHasConfigurationInterface(true);
53
KConfigGroup cg = config();
54
m_showBatteryString = cg.readEntry("showBatteryString", true);
55
m_drawBackground = cg.readEntry("drawBackground", true);
56
m_pixelSize = cg.readEntry("size", 200);
57
m_smallPixelSize = 22;
58
m_theme = new Plasma::Svg("widgets/battery", this);
59
m_theme->setContentType(Plasma::Svg::SingleImage);
60
m_theme->resize(m_pixelSize, m_pixelSize);
62
m_font = QApplication::font();
63
m_font.setWeight(QFont::Bold);
65
m_textColor = KColorScheme(QPalette::Active, KColorScheme::View).foreground().color();
66
m_boxColor = KColorScheme(QPalette::Active, KColorScheme::View).background().color();
69
m_boxHoverAlpha = 192;
70
m_boxColor.setAlpha(m_boxAlpha);
72
dataEngine("powermanagement")->connectSource(I18N_NOOP("Battery"), this);
73
dataEngine("powermanagement")->connectSource(I18N_NOOP("AC Adapter"), this);
75
updated(I18N_NOOP("Battery"), dataEngine("powermanagement")->query(I18N_NOOP("Battery")));
76
updated(I18N_NOOP("AC Adapter"), dataEngine("powermanagement")->query(I18N_NOOP("AC Adapter")));
78
setAcceptsHoverEvents(true);
79
if (formFactor() == Plasma::Vertical ||
80
formFactor() == Plasma::Horizontal) {
81
kDebug() << "Init:Small FormFactor";
83
kDebug() << "Init:Huge FormFactor";
87
QSizeF Battery::contentSizeHint() const
92
void Battery::constraintsUpdated(Plasma::Constraints constraints)
94
if (constraints & Plasma::FormFactorConstraint) {
96
if (formFactor() == Plasma::Vertical ||
97
formFactor() == Plasma::Horizontal) {
98
kDebug() << "Small FormFactor";
99
setDrawStandardBackground(true);
100
pixelSize = m_smallPixelSize;
102
kDebug() << "Huge FormFactor";
103
setDrawStandardBackground(m_drawBackground);
104
pixelSize = m_pixelSize;
106
m_theme->resize(QSize(pixelSize, pixelSize));
108
prepareGeometryChange();
109
m_size = m_theme->size();
110
m_font.setPointSize((int)(pixelSize/10));
115
void Battery::updated(const QString& source, const Plasma::DataEngine::Data &data)
117
kDebug() << "Applet::updated() ---------------------------- " << source;
118
if (source == I18N_NOOP("Battery")) {
119
m_hasBattery = data[I18N_NOOP("has Battery")].toBool();
120
if (!data[I18N_NOOP("Plugged in")].toBool()) {
121
m_hasBattery = false;
123
m_battery_percent = data[I18N_NOOP("Percent")].toInt();
124
m_battery_percent_label = data[I18N_NOOP("Percent")].toString();
125
m_battery_percent_label.append("%");
127
m_battery_percent_label = I18N_NOOP("No Battery");
129
kDebug() << "Applet::Battery::updated " << m_battery_percent;
130
} else if (source == I18N_NOOP("AC Adapter")) {
131
m_acadapter_plugged = data[I18N_NOOP("Plugged in")].toBool();
132
kDebug() << "Applet::AC Adapter updated: " << m_acadapter_plugged;
134
kDebug() << "Applet::Dunno what to do with " << source;
139
void Battery::showConfigurationInterface()
142
m_dialog = new KDialog;
143
m_dialog->setCaption(i18n("Configure Battery Monitor"));
145
ui.setupUi(m_dialog->mainWidget());
146
m_dialog->setButtons( KDialog::Ok | KDialog::Cancel | KDialog::Apply );
148
connect( m_dialog, SIGNAL(applyClicked()), this, SLOT(configAccepted()) );
149
connect( m_dialog, SIGNAL(okClicked()), this, SLOT(configAccepted()) );
152
ui.spinSize->setValue((int)m_size.width());
153
ui.showBatteryStringCheckBox->setChecked(m_showBatteryString ? Qt::Checked : Qt::Unchecked);
154
ui.drawBackgroundCheckBox->setChecked(m_drawBackground ? Qt::Checked : Qt::Unchecked);
158
void Battery::configAccepted()
160
KConfigGroup cg = config();
161
m_showBatteryString = ui.showBatteryStringCheckBox->checkState() == Qt::Checked;
162
cg.writeEntry("showBatteryString", m_showBatteryString);
164
m_drawBackground = ui.drawBackgroundCheckBox->checkState() == Qt::Checked;
165
cg.writeEntry("drawBackground", m_drawBackground);
166
cg.writeEntry("size", ui.spinSize->value());
168
kDebug() << "Resize to: " << ui.spinSize->value();
169
m_pixelSize = ui.spinSize->value();
170
m_theme->resize(m_pixelSize, m_pixelSize);
172
dataEngine("powermanagement")->disconnectSource(I18N_NOOP("Battery"), this);
173
dataEngine("powermanagement")->connectSource(I18N_NOOP("Battery"), this);
174
dataEngine("powermanagement")->disconnectSource(I18N_NOOP("AC Adapter"), this);
175
dataEngine("powermanagement")->connectSource(I18N_NOOP("AC Adapter"), this);
177
constraintsUpdated(Plasma::AllConstraints);
181
void Battery::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
188
void Battery::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
199
void Battery::paintLabel(QPainter *p, const QString& labelText)
201
// Store font size, we want to restore it shortly
202
int original_font_size = m_font.pointSize();
204
// Fonts smaller than 4 points become unreadable, so the minimum size is 6
205
if (original_font_size < KGlobalSettings::smallestReadableFont().pointSize()) {
206
m_font.setPointSize(KGlobalSettings::smallestReadableFont().pointSize());
208
QFontMetrics fm(m_font);
211
int text_width = fm.width(labelText);
213
// Longer texts get smaller fonts
214
if (labelText.length() > 4) {
215
if (original_font_size/1.5 < KGlobalSettings::smallestReadableFont().pointSize()) {
216
m_font.setPointSize((int)(KGlobalSettings::smallestReadableFont().pointSize()));
218
m_font.setPointSize((int)(original_font_size/1.5));
220
fm = QFontMetrics(m_font);
221
text_width = (int)(fm.width(labelText) * 1.2);
223
// Smaller texts get a wider box
224
text_width = (int)(text_width * 1.4);
228
// Let's find a good position for painting the background
229
QRect text_rect = QRect((int)((contentSize().width()-fm.width(labelText))/2),
230
(int)(((contentSize().height() - (int)fm.height())/2*0.9)),
232
(int)(fm.height()*1.2));
233
// Poor man's highlighting
235
m_boxColor.setAlpha(m_boxHoverAlpha);
237
p->setBrush(m_boxColor);
239
// Find sensible proportions for the rounded corners
240
float round_prop = text_rect.width() / text_rect.height();
242
// Tweak the rounding edge a bit with the proportions of the textbox
243
int round_radius = 35;
244
p->drawRoundRect(text_rect, (int)(round_radius/round_prop), round_radius);
246
p->setBrush(m_textColor);
247
p->drawText(text_rect, Qt::AlignCenter, labelText);
249
// Reset font and box
250
m_font.setPointSize(original_font_size);
251
m_boxColor.setAlpha(m_boxAlpha);
254
void Battery::paintInterface(QPainter *p, const QStyleOptionGraphicsItem *option, const QRect &contentsRect)
257
//Q_UNUSED( contentsRect );
259
bool showString = true;
261
if (formFactor() == Plasma::Vertical ||
262
formFactor() == Plasma::Horizontal) {
266
p->setRenderHint(QPainter::SmoothPixmapTransform);
267
p->setRenderHint(QPainter::Antialiasing);
270
m_theme->paint(p, contentsRect, "AcAdapter");
271
if (formFactor() == Plasma::Planar ||
272
formFactor() == Plasma::MediaCenter) {
274
// Show that there's no battery
275
paintLabel(p, m_battery_percent_label);
280
if (m_theme->elementExists("Battery")) {
281
m_theme->paint(p, contentsRect, "Battery");
283
kDebug() << "Battery does not exist in SVG";
286
// Now let's find out which fillstate to show
287
QString fill_element = QString();
289
if (m_battery_percent > 95) {
290
fill_element = "Fill100";
291
} else if (m_battery_percent > 90) {
292
fill_element = "Fill90";
293
} else if (m_battery_percent > 80) {
294
fill_element = "Fill80";
295
} else if (m_battery_percent > 70) {
296
fill_element = "Fill70";
297
} else if (m_battery_percent > 55) {
298
fill_element = "Fill60";
299
} else if (m_battery_percent > 40) {
300
fill_element = "Fill50";
301
} else if (m_battery_percent > 30) {
302
fill_element = "Fill40";
303
} else if (m_battery_percent > 20) {
304
fill_element = "Fill30";
305
} else if (m_battery_percent > 10) {
306
fill_element = "Fill20";
307
} else if (m_battery_percent >= 5) {
308
fill_element = "Fill10";
311
if (!fill_element.isEmpty()) {
312
if (m_theme->elementExists(fill_element)) {
313
m_theme->paint(p, contentsRect, fill_element);
315
kDebug() << fill_element << " does not exist in svg";
319
if (m_acadapter_plugged) {
320
m_theme->paint(p, contentsRect, "AcAdapter");
323
// Only show batterystring when we're huge
324
if (formFactor() == Plasma::Planar ||
325
formFactor() == Plasma::MediaCenter) {
326
showString = m_showBatteryString;
329
// For small FormFactors, we're drawing a shadow,
331
if (formFactor() == Plasma::Vertical ||
332
formFactor() == Plasma::Horizontal) {
333
m_theme->paint(p, contentsRect, "Shadow");
337
if (formFactor() == Plasma::Planar ||
338
formFactor() == Plasma::MediaCenter) {
339
if (showString || m_isHovered) {
340
// Show the charge percentage with a box
341
// on top of the battery, but only for plasmoids bigger than ....
342
if (m_pixelSize > 36) {
343
paintLabel(p, m_battery_percent_label);
349
#include "battery.moc"