~ubuntu-branches/ubuntu/gutsy/kdebase-workspace/gutsy

« back to all changes in this revision

Viewing changes to plasma/applets/battery/battery.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2007-10-11 14:04:48 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20071011140448-v0eb7lxbb24zagca
Tags: 3.94.0-0ubuntu1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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>               *
 
5
 *                                                                         *
 
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.                                   *
 
10
 *                                                                         *
 
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.                          *
 
15
 *                                                                         *
 
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
 ***************************************************************************/
 
21
 
 
22
#include "battery.h"
 
23
 
 
24
#include <QApplication>
 
25
#include <QPainter>
 
26
#include <QStyleOptionGraphicsItem>
 
27
#include <QFont>
 
28
#include <QGraphicsSceneHoverEvent>
 
29
 
 
30
#include <KDebug>
 
31
#include <KIcon>
 
32
#include <KLocalizedString>
 
33
#include <KSharedConfig>
 
34
#include <KDialog>
 
35
#include <KColorScheme>
 
36
#include <KGlobalSettings>
 
37
 
 
38
#include <plasma/svg.h>
 
39
#include <plasma/theme.h>
 
40
 
 
41
Battery::Battery(QObject *parent, const QVariantList &args)
 
42
    : Plasma::Applet(parent, args),
 
43
      m_battery_percent_label(0),
 
44
      m_battery_percent(0),
 
45
      m_dialog(0),
 
46
      m_isHovered(0),
 
47
      m_hasBattery(0)
 
48
{
 
49
    kDebug() << "Loading applet battery";
 
50
    setAcceptsHoverEvents(true);
 
51
    setHasConfigurationInterface(true);
 
52
 
 
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);
 
61
 
 
62
    m_font = QApplication::font();
 
63
    m_font.setWeight(QFont::Bold);
 
64
 
 
65
    m_textColor = KColorScheme(QPalette::Active, KColorScheme::View).foreground().color();
 
66
    m_boxColor = KColorScheme(QPalette::Active, KColorScheme::View).background().color();
 
67
 
 
68
    m_boxAlpha = 92;
 
69
    m_boxHoverAlpha = 192;
 
70
    m_boxColor.setAlpha(m_boxAlpha);
 
71
 
 
72
    dataEngine("powermanagement")->connectSource(I18N_NOOP("Battery"), this);
 
73
    dataEngine("powermanagement")->connectSource(I18N_NOOP("AC Adapter"), this);
 
74
 
 
75
    updated(I18N_NOOP("Battery"), dataEngine("powermanagement")->query(I18N_NOOP("Battery")));
 
76
    updated(I18N_NOOP("AC Adapter"), dataEngine("powermanagement")->query(I18N_NOOP("AC Adapter")));
 
77
 
 
78
    setAcceptsHoverEvents(true);
 
79
    if (formFactor() == Plasma::Vertical ||
 
80
        formFactor() == Plasma::Horizontal) {
 
81
        kDebug() << "Init:Small FormFactor";
 
82
    } else {
 
83
        kDebug() << "Init:Huge FormFactor";
 
84
    }
 
85
}
 
86
 
 
87
QSizeF Battery::contentSizeHint() const
 
88
{
 
89
    return m_size;
 
90
}
 
91
 
 
92
void Battery::constraintsUpdated(Plasma::Constraints constraints)
 
93
{
 
94
    if (constraints & Plasma::FormFactorConstraint) {
 
95
        int pixelSize = 0;
 
96
        if (formFactor() == Plasma::Vertical ||
 
97
                formFactor() == Plasma::Horizontal) {
 
98
            kDebug() << "Small FormFactor";
 
99
            setDrawStandardBackground(true);
 
100
            pixelSize = m_smallPixelSize;
 
101
        } else {
 
102
            kDebug() << "Huge FormFactor";
 
103
            setDrawStandardBackground(m_drawBackground);
 
104
            pixelSize = m_pixelSize;
 
105
        }
 
106
        m_theme->resize(QSize(pixelSize, pixelSize));
 
107
        updateGeometry();
 
108
        prepareGeometryChange();
 
109
        m_size = m_theme->size();
 
110
        m_font.setPointSize((int)(pixelSize/10));
 
111
        updateGeometry();
 
112
    }
 
113
}
 
114
 
 
115
void Battery::updated(const QString& source, const Plasma::DataEngine::Data &data)
 
116
{
 
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; 
 
122
        }
 
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("%");
 
126
        if (!m_hasBattery) {
 
127
            m_battery_percent_label = I18N_NOOP("No Battery");
 
128
        }
 
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; 
 
133
    } else {
 
134
        kDebug() << "Applet::Dunno what to do with " << source;
 
135
    }
 
136
    update();
 
137
}
 
138
 
 
139
void Battery::showConfigurationInterface()
 
140
{
 
141
     if (m_dialog == 0) {
 
142
        m_dialog = new KDialog;
 
143
        m_dialog->setCaption(i18n("Configure Battery Monitor"));
 
144
 
 
145
        ui.setupUi(m_dialog->mainWidget());
 
146
        m_dialog->setButtons( KDialog::Ok | KDialog::Cancel | KDialog::Apply );
 
147
 
 
148
        connect( m_dialog, SIGNAL(applyClicked()), this, SLOT(configAccepted()) );
 
149
        connect( m_dialog, SIGNAL(okClicked()), this, SLOT(configAccepted()) );
 
150
 
 
151
    }
 
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); 
 
155
    m_dialog->show();
 
156
}
 
157
 
 
158
void Battery::configAccepted()
 
159
{
 
160
    KConfigGroup cg = config();
 
161
    m_showBatteryString = ui.showBatteryStringCheckBox->checkState() == Qt::Checked;
 
162
    cg.writeEntry("showBatteryString", m_showBatteryString);
 
163
 
 
164
    m_drawBackground = ui.drawBackgroundCheckBox->checkState() == Qt::Checked;
 
165
    cg.writeEntry("drawBackground", m_drawBackground);
 
166
    cg.writeEntry("size", ui.spinSize->value());
 
167
 
 
168
    kDebug() << "Resize to: " << ui.spinSize->value();
 
169
    m_pixelSize = ui.spinSize->value();
 
170
    m_theme->resize(m_pixelSize, m_pixelSize);
 
171
 
 
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);
 
176
 
 
177
    constraintsUpdated(Plasma::AllConstraints);
 
178
    cg.config()->sync();
 
179
}
 
180
 
 
181
void Battery::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
 
182
{
 
183
    Q_UNUSED( event );
 
184
    m_isHovered = true;
 
185
    update();
 
186
}
 
187
 
 
188
void Battery::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
 
189
{
 
190
    Q_UNUSED( event );
 
191
    m_isHovered = false;
 
192
    update();
 
193
}
 
194
 
 
195
Battery::~Battery()
 
196
{
 
197
}
 
198
 
 
199
void Battery::paintLabel(QPainter *p, const QString& labelText)
 
200
{
 
201
    // Store font size, we want to restore it shortly
 
202
    int original_font_size = m_font.pointSize();
 
203
 
 
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());
 
207
    }
 
208
    QFontMetrics fm(m_font);
 
209
 
 
210
 
 
211
    int text_width = fm.width(labelText);
 
212
 
 
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()));
 
217
        } else {
 
218
            m_font.setPointSize((int)(original_font_size/1.5));
 
219
        }
 
220
        fm = QFontMetrics(m_font);
 
221
        text_width = (int)(fm.width(labelText) * 1.2);
 
222
    } else {
 
223
        // Smaller texts get a wider box
 
224
        text_width = (int)(text_width * 1.4);
 
225
    }
 
226
    p->setFont(m_font);
 
227
 
 
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)),
 
231
                            text_width, 
 
232
                            (int)(fm.height()*1.2));
 
233
    // Poor man's highlighting
 
234
    if (m_isHovered) {
 
235
        m_boxColor.setAlpha(m_boxHoverAlpha);
 
236
    }
 
237
    p->setBrush(m_boxColor);
 
238
    
 
239
    // Find sensible proportions for the rounded corners
 
240
    float round_prop = text_rect.width() / text_rect.height();
 
241
 
 
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);
 
245
 
 
246
    p->setBrush(m_textColor);
 
247
    p->drawText(text_rect, Qt::AlignCenter, labelText);
 
248
 
 
249
    // Reset font and box
 
250
    m_font.setPointSize(original_font_size);
 
251
    m_boxColor.setAlpha(m_boxAlpha);
 
252
}
 
253
 
 
254
void Battery::paintInterface(QPainter *p, const QStyleOptionGraphicsItem *option, const QRect &contentsRect)
 
255
{
 
256
    Q_UNUSED( option );
 
257
    //Q_UNUSED( contentsRect );
 
258
 
 
259
    bool showString = true;
 
260
 
 
261
    if (formFactor() == Plasma::Vertical ||
 
262
        formFactor() == Plasma::Horizontal) {
 
263
        showString = false;
 
264
    };
 
265
 
 
266
    p->setRenderHint(QPainter::SmoothPixmapTransform);
 
267
    p->setRenderHint(QPainter::Antialiasing);
 
268
 
 
269
    if (!m_hasBattery) {
 
270
        m_theme->paint(p, contentsRect, "AcAdapter");
 
271
        if (formFactor() == Plasma::Planar ||
 
272
            formFactor() == Plasma::MediaCenter) {
 
273
 
 
274
            // Show that there's no battery
 
275
            paintLabel(p, m_battery_percent_label);
 
276
        }
 
277
        return;
 
278
    }
 
279
 
 
280
    if (m_theme->elementExists("Battery")) {
 
281
        m_theme->paint(p, contentsRect, "Battery");
 
282
    } else {
 
283
        kDebug() << "Battery does not exist in SVG";
 
284
    }
 
285
 
 
286
    // Now let's find out which fillstate to show
 
287
    QString fill_element = QString();
 
288
 
 
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";
 
309
    }
 
310
 
 
311
    if (!fill_element.isEmpty()) {
 
312
        if (m_theme->elementExists(fill_element)) {
 
313
            m_theme->paint(p, contentsRect, fill_element);
 
314
        } else {
 
315
            kDebug() << fill_element << " does not exist in svg";
 
316
        }
 
317
    }
 
318
 
 
319
    if (m_acadapter_plugged) {
 
320
        m_theme->paint(p, contentsRect, "AcAdapter");
 
321
    }
 
322
 
 
323
    // Only show batterystring when we're huge
 
324
    if (formFactor() == Plasma::Planar ||
 
325
        formFactor() == Plasma::MediaCenter) {
 
326
        showString = m_showBatteryString;
 
327
    }
 
328
 
 
329
    // For small FormFactors, we're drawing a shadow,
 
330
    // but no text.
 
331
    if (formFactor() == Plasma::Vertical ||
 
332
        formFactor() == Plasma::Horizontal) {
 
333
        m_theme->paint(p, contentsRect, "Shadow");
 
334
        showString = false;
 
335
    }
 
336
 
 
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);
 
344
            }
 
345
        }
 
346
    }
 
347
}
 
348
 
 
349
#include "battery.moc"